package com.smartmore.test;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

public class ImageGridView extends ViewGroup {

	 public ImageGridView(Context context) {
	        super(context);
	        init();
	    }

	    public ImageGridView(Context context, AttributeSet attrs) {
	        this(context, attrs, 0);
	        init();
	    }

	    public ImageGridView(Context context, AttributeSet attrs, int defStyle) {
	        super(context, attrs, defStyle);
	        init();
	    }
	    
	    public void init() {
	    	
	    }
	    
	    @Override
	    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	    	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	    	
	        final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
	        final int widthSpecSize =  MeasureSpec.getSize(widthMeasureSpec);
	        

	        final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
	        final int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);

	        if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
	            throw new RuntimeException("GridLayout cannot have UNSPECIFIED dimensions");
	        }

	        final int width = widthSpecSize - getPaddingLeft() - getPaddingRight();
	        final int height = heightSpecSize - getPaddingTop() - getPaddingBottom();
	        
	        final int columnWidth  = width /4;
	        final int rowHeight = height / 4;

	        final int count = getChildCount();
	        for (int i = 0; i < count; i++) {
	            final View child = getChildAt(i);
	            
	            RectImageView rectImageView = (RectImageView)child;
	            Rect rect = rectImageView.getmImageSizeRect();
	            int childWidthSpec = MeasureSpec.makeMeasureSpec(rect.width(), MeasureSpec.EXACTLY);
	            int childheightSpec = MeasureSpec.makeMeasureSpec(rect.height(), MeasureSpec.EXACTLY);
	            


//	            if(i == 0) {
//		            int childWidthSpec = MeasureSpec.makeMeasureSpec(200, MeasureSpec.EXACTLY);
//		            int childheightSpec = MeasureSpec.makeMeasureSpec(200, MeasureSpec.EXACTLY);
//		            
//		            Log.d("--","-------onMeasure-------"+ MeasureSpec.getSize(childWidthSpec)+","+heightSpecSize);
//	            }else {
//		            int childWidthSpec = MeasureSpec.makeMeasureSpec(200, MeasureSpec.EXACTLY);
//		            int childheightSpec = MeasureSpec.makeMeasureSpec(280, MeasureSpec.EXACTLY);
//		            Log.d("--","-------onMeasure-------"+MeasureSpec.getSize(childWidthSpec)+","+heightSpecSize);	            	
//	            }
	        }
	       
	        //setMeasuredDimension(widthSpecSize, heightSpecSize);
	        
	    }	    
	    
	    @Override
	    protected void onLayout(boolean changed, int l, int t, int r, int b) {
	        
	        final int paddingLeft = getPaddingLeft();
	        final int paddingTop = getPaddingTop();
	        final int count = getChildCount();
	        Log.d("--","##"+l+","+t+","+r+","+b);
	        for (int i = 0; i < count; i++) {
	            View child = getChildAt(i);
	            if (child.getVisibility() != GONE) {
	            	RectImageView rectImageView = (RectImageView)child;
	            	Rect rect = rectImageView.getmImageSizeRect();
	            	child.layout(rect.left, rect.top, rect.right,rect.bottom);
	            	
	               
	            	
//	                if(i == 0) {
//	                	Log.d("--","--------1------"+child.getMeasuredWidth()+","+child.getMeasuredHeight());
//	                	child.layout(0, 0, 200,200);
//	                }else if(i == 1) {
//	                	Log.d("--","-------2-------"+child.getMeasuredWidth()+","+child.getMeasuredHeight());
//	                	child.layout(0, 200, 200,480);	                	
//	                }
//	                else {
//	                	Log.d("--","-------3-------"+child.getMeasuredWidth()+","+child.getMeasuredHeight());
//	                	child.layout(200, 0, 480,480);	                	
//	                }
	            }
	        }
	    }	    
	    
	   
	
}
